package se.perp.c4.control;

import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import se.perp.c4.model.ActorCollection;
import se.perp.c4.model.TerritoryCollection;
import se.perp.c4.ui.ActorViewer;
import se.perp.c4.ui.MainMenu;
import se.perp.c4.ui.MainWindow;
import se.perp.c4.ui.PieceCollection;

public class Main {

    public static final String VERSION = "1.1.4";
    public static final String BUILD_DATE = "March 26, 2009";
    private static Properties properties;
    public static int MAX_NAME_LENGTH = 16;
    public static boolean IS_TEST_VERSION = false;

    private static Properties createDefaultProperties() {
        Properties p = new Properties();
        p.setProperty("ip", "127.0.0.1");
        p.setProperty("port", "11234");
        p.setProperty("sound", "false");
        p.setProperty("popup", "false");
        p.setProperty("centermsg", "true");
        p.setProperty("specialcardreminder", "true");
        p.setProperty("movementassist", "true");
        p.setProperty("playername", "Player");
        p.setProperty("shelobrule", "SHELOB_DIES");
        return p;
    }

    public static void main(String[] args) {

        try {
            properties = new Properties();
            properties.loadFromXML(new FileInputStream("confrontation.xml"));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).warning("Could not read property file.");
            properties = createDefaultProperties();
        }

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }

        String verStr = System.getProperty("java.version");
        String subStr = verStr.substring(0, 3);
        try {
            double d = Double.parseDouble(subStr);
            if (Double.compare(d, 1.6) < 0) {
                JOptionPane.showMessageDialog(null, "You need at least version 1.6 of the Java runtime\n" +
                        "to run Confrontation. Sorry.\n\n" +
                        "Detected version: " + verStr, "Incompatible Java runtime", JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Could not determine whether you have a compatible Java version.\n" +
                    "The application may crash without warning. Just so you know.", "Warning", JOptionPane.WARNING_MESSAGE);
        }

        ActorCollection actorColl = new ActorCollection();
        TerritoryCollection terrColl = new TerritoryCollection();
        PieceCollection pieceColl = new PieceCollection();
        pieceColl.setAllVisible(false);

        ActorViewer actorViewer = new ActorViewer();

        MainMenu menuBar = new MainMenu(properties);

        MainWindow m = new MainWindow(actorViewer, menuBar);
        actorViewer.setVisible(false);

        MainWindowListener mainWindowListener = new MainWindowListener(
                m, pieceColl, terrColl, actorColl, actorViewer, properties);
        menuBar.setMenuListener(mainWindowListener);
        m.addWindowListener(mainWindowListener);

        // Center main window on desktop and show.
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle maxBounds = ge.getMaximumWindowBounds();
        int mwx = (maxBounds.width - m.getWidth()) / 2;
        int mwy = (maxBounds.height - m.getHeight()) / 2;
        m.setLocation(mwx, mwy);
        m.setVisible(true);
    }
}
